﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication9.Models
{
    public class CRUD
    {
        public static User Retrieve(User UserInfo)
        {
            using (MainModelContainer context = new MainModelContainer())
            {
                try
                {

                    User find;
                    find = context.Users.FirstOrDefault(user => user.Login == UserInfo.Login);
                    return find;
                }
                catch
                {
                    return null;
                }
            }
        }

        public static User RetrieveID(User UserInfo)
        {
            using (MainModelContainer context = new MainModelContainer())
            {
                try
                {

                    User find;
                    find = context.Users.FirstOrDefault(user => user.Id == UserInfo.Id);
                    return find;
                }
                catch
                {
                    return null;
                }
            }
        }
        public static bool Create(User UserInfo)
        {
            using (MainModelContainer context = new MainModelContainer())
            {
                try
                {

                    context.Users.AddObject(UserInfo);
                    context.SaveChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public static bool Delete(User UserInfo)
        {
            using (MainModelContainer context = new MainModelContainer())
            {

                try
                {
                    User foo = Retrieve(UserInfo);
                    context.Users.DeleteObject(foo);
                    context.SaveChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public static bool Update(User UserInfo)
        {
            using (MainModelContainer context = new MainModelContainer())
            {
                try
                {
                    User foo = RetrieveID(UserInfo);

                    foo.BDate = UserInfo.BDate;
                    foo.Email = UserInfo.Email;
                    foo.FName = UserInfo.FName;
                    foo.Login = UserInfo.Login;
                    foo.Password = UserInfo.Password;
                    foo.Phone = UserInfo.Phone;
                    foo.Role = UserInfo.Role;
                    foo.Salt = UserInfo.Salt;
                    foo.SName = UserInfo.SName;

                    context.SaveChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
    }

    public static class DataService
    {
        public static User GetUserId(int id)
        {
            User foo = new User();
            foo.Id = id;
            foo = CRUD.RetrieveID(foo);
            return foo;
        }
    }
}
